/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.siglo21.ppo.persistence.dao;

import ar.com.siglo21.ppo.model.Personal;
import ar.com.siglo21.ppo.persistence.MySQLConnection;
import ar.com.siglo21.ppo.util.TrabajoIntegradorException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PersonalDAOImpl implements PersonalDAO {

    @Override
    public void agregar(Personal object) throws TrabajoIntegradorException, SQLException {
        String sql = "Insert into personal(nombre,apellido,email,dni,usuario,password,rol) values "
                + "('" + object.getNombre()+  "','"+object.getApellido()+"','"+object.getEmail()+"','"+object.getDni()+"'"
                + ",'"+object.getUsuario()+"','"+object.getPassword()+"','"+object.getRol()+"')";
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public boolean existe(Personal object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void eliminar(Personal object) throws TrabajoIntegradorException, SQLException {

        String sql = "DELETE FROM personal where id=" + object.getId();
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public Personal leer(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void actualizar(Personal object) throws TrabajoIntegradorException, SQLException {
        String sql="UPDATE personal SET nombre='"+object.getNombre()+"',"+ "apellido='"+object.getApellido()+"',"+"email='"+object.getEmail()+"',"+"dni="+object.getDni()+","+"usuario='"+object.getUsuario()+"',"+"password='"+object.getPassword()+"',"+"rol='"+object.getRol()+"' where id="+object.getId();
       MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public List<Personal> listarPersonal() throws SQLException, TrabajoIntegradorException {
        String sql = "Select * from personal";
        MySQLConnection.getDbCon().query(sql);
        Personal personal;

        List<Personal> listaPersonal = new ArrayList();
        ResultSet resulset = MySQLConnection.getDbCon().query(sql);

        while (resulset.next()) {
            personal = new Personal();
            personal.setId(resulset.getInt(1));
            personal.setNombre(resulset.getString(2));//nombre
            personal.setApellido(resulset.getString(3));//apellido
            personal.setEmail(resulset.getString(4));//email
            personal.setDni(resulset.getInt(5));//dni
            personal.setUsuario(resulset.getString(6));//usuario
            personal.setPassword(resulset.getString(7));//password
            personal.setRol(resulset.getString(8));//rol
            listaPersonal.add(personal);
        }

        return listaPersonal;

    }

}
